﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.Diagnostics;

namespace Loader.Client {
    static class Program {

        static bool Is64BitProcess {
            get {
                //TODO: .NET 4.0 ve üstü için Environment.Is64BitProcess kullanılmalı.
                return IntPtr.Size == 8;
            }
        }

        [STAThread]
        static void Main(string[] args) {
            string mode = "";
            string message = "";
            string application = "";
            string arguments = "";
            string UpdateDirectory = "";
            string LoaderUpdateApplication = "";
            string ServerRunAs= "";
            string ClientRunAs = "";

            if (args.Length > 0)
                for (int i = 0; i < args.Length; i++)
                    arguments += args[i] + " ";

            string workDir = "";
            try {
                #region Load Config
                LoaderUpdateApplication = System.Configuration.ConfigurationSettings.AppSettings["LoaderUpdateApplication"].ToString();
                if (Is64BitProcess)
                    application = System.Configuration.ConfigurationSettings.AppSettings["LoaderApplication64"].ToString();
                else
                    application = System.Configuration.ConfigurationSettings.AppSettings["LoaderApplication"].ToString();

                #endregion

                #region relativePathApplication
                bool relativePathApplication = true;
                if (application.Length >= 3) {
                    if (application[1] == ':' && application[2] == '\\') { //Bir sürücü ise c:\ gibi
                        relativePathApplication = false;
                    }
                }
                if (application.Length >= 2) {
                    if (application[0] == '\\' && application[1] == '\\') { // network adresi ise \\SqlCluster gibi
                        relativePathApplication = false;
                    }
                }

                if (relativePathApplication) {
                    if (application.StartsWith("\\")) {
                        if (Application.StartupPath.EndsWith("\\")) {
                            application = Application.StartupPath + application.Substring(1);
                        } else {
                            application = Application.StartupPath + application;
                        }
                    } else {
                        if (Application.StartupPath.EndsWith("\\")) {
                            application = Application.StartupPath + application;
                        } else {
                            application = Application.StartupPath + "\\" + application;
                        }
                    }
                }
                #endregion

                if (arguments.Length == 0)
                    arguments = System.Configuration.ConfigurationSettings.AppSettings["LoaderArguments"].ToString();
                #region workDir
                if (Is64BitProcess)
                    workDir = System.Configuration.ConfigurationSettings.AppSettings["LoaderWorkingDirectory64"].ToString();
                else
                    workDir = System.Configuration.ConfigurationSettings.AppSettings["LoaderWorkingDirectory"].ToString();

                bool relativePathWorkPath = true;
                if (workDir.Length >= 3) {
                    if (workDir[1] == ':' && workDir[2] == '\\') { //Bir sürücü ise c:\ gibi
                        relativePathWorkPath = false;
                    }
                }
                if (workDir.Length >= 2) {
                    if (workDir[0] == '\\' && workDir[1] == '\\') { // network adresi ise \\SqlCluster gibi
                        relativePathWorkPath = false;
                    }
                }

                if (relativePathWorkPath) {
                    if (workDir.StartsWith("\\")) {
                        if (Application.StartupPath.EndsWith("\\")) {
                            workDir = Application.StartupPath + workDir.Substring(1);
                        } else {
                            workDir = Application.StartupPath + workDir;
                        }
                    } else {
                        if (Application.StartupPath.EndsWith("\\")) {
                            workDir = Application.StartupPath + workDir;
                        } else {
                            workDir = Application.StartupPath + "\\" + workDir;
                        }
                    }
                }
                #endregion

                #region UpdateDirectory
                if (Is64BitProcess)
                    UpdateDirectory = System.Configuration.ConfigurationSettings.AppSettings["UpdateDirectory64"].ToString();
                else
                    UpdateDirectory = System.Configuration.ConfigurationSettings.AppSettings["UpdateDirectory"].ToString();
                bool relativePathUpdateDirectoryPath = true;
                if (UpdateDirectory.Length >= 3)
                    if (UpdateDirectory[1] == ':' && UpdateDirectory[2] == '\\') { //Bir sürücü ise c:\ gibi
                        relativePathUpdateDirectoryPath = false;
                    }
                if (UpdateDirectory.Length >= 2) {
                    if (UpdateDirectory[0] == '\\' && UpdateDirectory[1] == '\\') { // network adresi ise \\SqlCluster gibi
                        relativePathUpdateDirectoryPath = false;
                    }
                }

                if (relativePathUpdateDirectoryPath) {
                    if (UpdateDirectory.StartsWith("\\")) {
                        if (Application.StartupPath.EndsWith("\\")) {
                            UpdateDirectory = Application.StartupPath + UpdateDirectory.Substring(1);
                        } else {
                            UpdateDirectory = Application.StartupPath + UpdateDirectory;
                        }
                    } else {
                        if (Application.StartupPath.EndsWith("\\")) {
                            UpdateDirectory = Application.StartupPath + UpdateDirectory;
                        } else {
                            UpdateDirectory = Application.StartupPath + "\\" + UpdateDirectory;
                        }
                    }
                }
                #endregion
                mode = System.Configuration.ConfigurationSettings.AppSettings["LoaderMode"].ToString();
                message = System.Configuration.ConfigurationSettings.AppSettings["LoaderMessage"].ToString();
                ServerRunAs = System.Configuration.ConfigurationSettings.AppSettings["ServerRunAs"].ToString();
                ClientRunAs = System.Configuration.ConfigurationSettings.AppSettings["ClientRunAs"].ToString();
            } catch (Exception e) { MessageBox.Show(e.Message + " " + e.InnerException); } 
            Process process = null;
            try {
                if (!string.IsNullOrEmpty(UpdateDirectory) && System.IO.Directory.Exists(UpdateDirectory)) {
                    if (!string.IsNullOrEmpty(LoaderUpdateApplication) && System.IO.File.Exists(LoaderUpdateApplication)) {
                        if (!string.IsNullOrEmpty(workDir) && System.IO.Directory.Exists(workDir)) {
                            process = new Process();
                            process.StartInfo.Arguments = workDir;
                            process.StartInfo.FileName = LoaderUpdateApplication;
                            process.StartInfo.CreateNoWindow = true;
                            process.StartInfo.WorkingDirectory = UpdateDirectory;
                            if(!string.IsNullOrEmpty(ServerRunAs))
                                process.StartInfo.Verb = "runas";      // farklı çalıştır yönetici olarak çalıştır
                            process.Start();
                            process.WaitForExit();
                        } else
                            MessageBox.Show("Configdeki LoaderWorkingDirectory adresi Bulunamadı!");
                    } else
                        MessageBox.Show("Configdeki LoaderUpdateApplication adresi Bulunamadı!");
                } else
                    MessageBox.Show("Configdeki UpdateDirectory adresi Bulunamadı!");
            } catch (Exception e) { MessageBox.Show(e.Message + " " + e.InnerException); } 
           
            process = new Process();
            process.StartInfo.Arguments = arguments;
            process.StartInfo.FileName = application;
            process.StartInfo.CreateNoWindow = true;
            process.StartInfo.WorkingDirectory = workDir;
            if (!string.IsNullOrEmpty(ClientRunAs))
                process.StartInfo.Verb = "runas";      // farklı çalıştır yönetici olarak çalıştır

            try {
                process.Start();
            } catch (Exception ex) {
                MessageBox.Show("Program Çalıştırılamadı.\n" + application + "\n" + workDir + "\n" + arguments + "\n\n" + ex.Message);
            }
        }
    }
}
